﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SubSonic;
using System.Data;
using System.Data.SqlClient;

public partial class User_Employer_DeleteDB : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Lblstatus.Text = "";
        Lblstatus2.Text = "";
        Lblstatus1.Text = "";
        Button1.Attributes.Add("onclick", "return confirm('هشدار : آيا از حذف بانک اطلاعاتی مطمئن هستيد؟');");
        if (!IsPostBack)
        {

            string year = (DateTime.Now.Year - 622).ToString();

            TxtYear.Text = year;
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        try
        {
            Query qad = new Query(DataAccess.TblContract.Schema);
            qad.QueryType = SubSonic.QueryType.Delete;
            qad.WHERE(DataAccess.TblContract.Columns.Year, TxtYear.Text);
            qad.BuildDeleteCommand();
            qad.Execute();
            Lblstatus.Text = " <script>alert('.بانک اطلاعاتی سال مورد نظر شما حذف گردید ')</script>";
        }
        catch
        {
            Lblstatus.Text = " <script>alert('.حذف بانک اطلاعاتی سال مورد نظر شما با خطا مواجه شد ')</script>";
        }
    }
    protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        GridView1.DataSource = "";
        GridView1.DataBind();
        GridView2.DataSource = "";
        GridView2.DataBind();
        if (RadioButtonList1.SelectedIndex == 0)
            MultiView1.ActiveViewIndex = 0;
        if (RadioButtonList1.SelectedIndex == 1)
            MultiView1.ActiveViewIndex = 1;
        if (RadioButtonList1.SelectedIndex == 2)
            MultiView1.ActiveViewIndex = 2;
        //if (RadioButtonList1.SelectedIndex == 3)
        //    MultiView1.ActiveViewIndex = 3;
        //if (RadioButtonList1.SelectedIndex == 4)
        //    MultiView1.ActiveViewIndex = 4;
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        GridView1.DataSource = DataAccess.TblContract.FetchByParameter(DataAccess.TblContract.Columns.ContractNumber, SubSonic.Comparison.Like, "%" + TxtNumber.Text + "%",OrderBy.Asc("ContractNumber"));
        GridView1.DataBind();
    }
    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        int id = Convert.ToInt32(e.CommandArgument);
        switch (e.CommandName)
        {
            case "Select":
                Guid idc = new Guid(GridView1.DataKeys[id].Value.ToString());

                Query qad = new Query(DataAccess.TblContract.Schema);
            qad.QueryType = SubSonic.QueryType.Delete;
            qad.WHERE(DataAccess.TblContract.Columns.Id,idc.ToString());
            qad.BuildDeleteCommand();
            qad.Execute();
            Lblstatus1.Text = " <script>alert('.بانک اطلاعاتی قرارداد مورد نظر شما حذف گردید ')</script>";
            GridView1.DataSource = DataAccess.TblContract.FetchByParameter(DataAccess.TblContract.Columns.ContractNumber, SubSonic.Comparison.Like, "%" + TxtNumber.Text + "%", OrderBy.Asc("ContractNumber"));
        GridView1.DataBind();
                //return Contractid;
                break;
        }
    }

    protected void GridView2_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        int id = Convert.ToInt32(e.CommandArgument);
        switch (e.CommandName)
        {
            case "Select":
                Guid idc = new Guid(GridView2.DataKeys[id].Value.ToString());

                Query qad = new Query(DataAccess.TblContract.Schema);
                qad.QueryType = SubSonic.QueryType.Delete;
                qad.WHERE(DataAccess.TblContract.Columns.Id, idc.ToString());
                qad.BuildDeleteCommand();
                qad.Execute();
                Lblstatus2.Text = " <script>alert('.بانک اطلاعاتی روستای مورد نظر شما حذف گردید ')</script>";
                GridView2.DataSource = DataAccess.TblContract.FetchByParameter(DataAccess.TblContract.Columns.VillageId, DropDownVillage.SelectedValue, OrderBy.Asc("ContractNumber"));
                GridView2.DataBind();
                //return Contractid;
                break;
        }
    }
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {

        if (e.Row.RowType == DataControlRowType.DataRow)
        {
          e.Row.Cells[13].Attributes.Add("onclick", "return confirm('هشدار : آيا از حذف بانک اطلاعاتی مطمئن هستيد؟');");
          Business.Calender cal = new Business.Calender();
            string pdate=cal.MiladiToPersian(Convert.ToDateTime( e.Row.Cells[1].Text));
            e.Row.Cells[1].Text=pdate;
          IDataReader t= DataAccess.TblUserDetail.FetchByParameter( DataAccess.TblUserDetail.Columns.UserId, e.Row.Cells[9].Text);
          if (t.Read())
          {
              e.Row.Cells[2].Text = t["Name"].ToString() + " / " + t["Family"].ToString();
              t.Close();
          }
         
          if (e.Row.Cells[3].Text == bool.TrueString)
              e.Row.Cells[3].Text = "قرارداد مشاوره";
            if(e.Row.Cells[3].Text==bool.FalseString)
                e.Row.Cells[3].Text="قرارداد نقشه برداری";
           DataAccess.TblVillage vil= DataAccess.TblVillage.FetchByID(e.Row.Cells[11].Text);
           e.Row.Cells[5].Text = vil.Name;
           DataAccess.TblDehestan deh= DataAccess.TblDehestan.FetchByID(vil.DehstanId);
           e.Row.Cells[6].Text = deh.Name;
           DataAccess.TblBakhsh bakhsh = DataAccess.TblBakhsh.FetchByID(deh.BakhshId);
           e.Row.Cells[7].Text = bakhsh.Name;
           DataAccess.TblCity city = DataAccess.TblCity.FetchByID(bakhsh.CityId);
           e.Row.Cells[8].Text = city.Name;
           e.Row.Cells[9].Visible = false;
           e.Row.Cells[11].Visible = false;
        }
    }
    protected void DropDownVillage_SelectedIndexChanged(object sender, EventArgs e)
    {
        SubSonic.Query q1 = DataAccess.TblContract.CreateQuery();
        q1.QueryType = SubSonic.QueryType.Select;
        q1.AddWhere(DataAccess.TblContract.Columns.VillageId,DropDownVillage.SelectedValue);
        q1.OrderBy = OrderBy.Asc("ContractNumber");
        GridView2.DataSource= q1.ExecuteReader();
        GridView2.DataBind();

                
    }
    protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Cells[13].Attributes.Add("onclick", "return confirm('هشدار : آيا از حذف بانک اطلاعاتی مطمئن هستيد؟');");
            Business.Calender cal = new Business.Calender();
            string pdate = cal.MiladiToPersian(Convert.ToDateTime(e.Row.Cells[1].Text));
            e.Row.Cells[1].Text = pdate;
            IDataReader t = DataAccess.TblUserDetail.FetchByParameter(DataAccess.TblUserDetail.Columns.UserId, e.Row.Cells[9].Text);
            if (t.Read())
            {
                e.Row.Cells[2].Text = t["Name"].ToString() + " / " + t["Family"].ToString();
                t.Close();
            }

            if (e.Row.Cells[3].Text == bool.TrueString)
                e.Row.Cells[3].Text = "قرارداد مشاوره";
            if (e.Row.Cells[3].Text == bool.FalseString)
                e.Row.Cells[3].Text = "قرارداد نقشه برداری";
            DataAccess.TblVillage vil = DataAccess.TblVillage.FetchByID(e.Row.Cells[11].Text);
            e.Row.Cells[5].Text = vil.Name;
            DataAccess.TblDehestan deh = DataAccess.TblDehestan.FetchByID(vil.DehstanId);
            e.Row.Cells[6].Text = deh.Name;
            DataAccess.TblBakhsh bakhsh = DataAccess.TblBakhsh.FetchByID(deh.BakhshId);
            e.Row.Cells[7].Text = bakhsh.Name;
            DataAccess.TblCity city = DataAccess.TblCity.FetchByID(bakhsh.CityId);
            e.Row.Cells[8].Text = city.Name;
            e.Row.Cells[9].Visible = false;
            e.Row.Cells[11].Visible = false;
        }
    }
}